package com.loong.musicdemo.ui.video.activity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;

import com.loong.musicdemo.util.dataDeal.MvDeal;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.RecyclerView;

import android.view.View;

import com.loong.musicdemo.R;
import com.loong.musicdemo.model.MV;
import com.loong.musicdemo.adapter.NormalMVAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 精选MV页面,用于显示各地区的热门MV数据
 */
public class MoreMvActivity extends AppCompatActivity {
    //网易出品
    private static final List<MV> mvNetease = new ArrayList<>();
    private static final List<MV> mvInlined = new ArrayList<>();
    private static final List<MV> mvOumei = new ArrayList<>();
    private static final List<MV> mvGangtai = new ArrayList<>();
    private static final List<MV> mvKorea = new ArrayList<>();
    private static final List<MV> mvJapan = new ArrayList<>();
    //适配器
    private static final NormalMVAdapter adapterNetease = new NormalMVAdapter(mvNetease);
    private static final NormalMVAdapter adapterInlined = new NormalMVAdapter(mvInlined);
    private static final NormalMVAdapter adapterOumei = new NormalMVAdapter(mvOumei);
    private static final NormalMVAdapter adapterGangtai = new NormalMVAdapter(mvGangtai);
    private static final NormalMVAdapter adapterKorea = new NormalMVAdapter(mvKorea);
    private static final NormalMVAdapter adapterJapan = new NormalMVAdapter(mvJapan);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //背景图片融合
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        setContentView(R.layout.activity_more_mv);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setTitle("精选MV");
            actionBar.setDisplayHomeAsUpEnabled(true);//若工具栏可用,则显示Home菜单
        }
        initView();//匹配视图
        new InitMVData().execute();
    }

    private void initView() {
        RecyclerView recyclerNetease = findViewById(R.id.mv_netease);
        recyclerNetease.setAdapter(adapterNetease);

        RecyclerView recyclerInlined = findViewById(R.id.mv_inlined);
        recyclerInlined.setAdapter(adapterInlined);

        RecyclerView recyclerOumei = findViewById(R.id.mv_oumei);
        recyclerOumei.setAdapter(adapterOumei);

        RecyclerView recyclerKorea = findViewById(R.id.mv_korea);
        recyclerKorea.setAdapter(adapterKorea);

        RecyclerView recyclerJapan = findViewById(R.id.mv_japan);
        recyclerJapan.setAdapter(adapterJapan);

        RecyclerView recyclerGangtai = findViewById(R.id.mv_gangtai);
        recyclerGangtai.setAdapter(adapterGangtai);

    }

    /**
     * 初始化数据
     */
    static class InitMVData extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... voids) {
            int limit = 4;//每个获取四条信息
            //刷新数据
            List<MV> mvList;
            mvList = MvDeal.getMvByAllInfo("全部", "网易出品", "上升最快", limit, 0);
            if (mvList != null) {
                mvNetease.addAll(mvList);
            }
            mvList = MvDeal.getMVByOffsetAndArea("港台", 0, limit);
            if (mvList!=null){
                mvGangtai.addAll(mvList);
            }
            mvList = MvDeal.getMVByOffsetAndArea("欧美", 0, limit);
            if (mvList!=null){
                mvOumei.addAll(mvList);
            }
            mvList = MvDeal.getMVByOffsetAndArea("日本", 0, limit);
            if (mvList!=null){
                mvJapan.addAll(mvList);
            }
            mvList = MvDeal.getMVByOffsetAndArea("韩国", 0, limit);
            if (mvList!=null){
                mvKorea.addAll(mvList);
            }
            mvList = MvDeal.getMVByOffsetAndArea("内地", 0, limit);
            if (mvList!=null){
                mvInlined.addAll(mvList);
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            adapterNetease.notifyDataSetChanged();
            adapterInlined.notifyDataSetChanged();
            adapterKorea.notifyDataSetChanged();
            adapterJapan.notifyDataSetChanged();
            adapterGangtai.notifyDataSetChanged();
            adapterOumei.notifyDataSetChanged();
        }
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
